﻿@using NuGetGallery
@using NuGetGallery.Areas.Admin.Models
@using NuGet.Services.Validation
@model ValidationPageViewModel
@{
    ViewBag.Title = "Package Validation";
}

@helper ShowTimestamp(DateTimeOffset? timestamp)
{
    if (timestamp.HasValue)
    {
        <span title="@(timestamp.Value.UtcDateTime.ToString("O"))Z">@timestamp.Value.UtcDateTime.ToString("G")</span>
    }
}

<section role="main" class="container main-container">
    <h2>Package Validations</h2>

    <div>
        <a href="@Url.Action(actionName:"Pending")">Get pending validations</a>
        |
        @ViewHelpers.PostLink(
            this,
            formId: "revalidate-pending-packages",
            linkText: "Revalidate pending packages",
            actionName: "RevalidatePending",
            controllerName: "Validation",
            role: string.Empty,
            area: "Admin",
            classes: "form-inline-empty",
            formValues: new Dictionary<string, string>
            {
                { "validatingType", ValidatingType.Package.ToString() },
            })
        |
        @ViewHelpers.PostLink(
            this,
            formId: "revalidate-pending-symbol-packages",
            linkText: "Revalidate pending symbol packages",
            actionName: "RevalidatePending",
            controllerName: "Validation",
            role: string.Empty,
            area: "Admin",
            classes: "form-inline-empty",
            formValues: new Dictionary<string, string>
            {
                { "validatingType", ValidatingType.SymbolPackage.ToString() },
            })
    </div>

    <form method="get" action="@Url.Action(actionName: "Search")">
        <p>
            <textarea name="q" placeholder="Search for package validations..." autocomplete="off" autofocus style="width: 100%" rows="5" class="textarea-brand">@Model.Query</textarea>
        </p>
        <p>
            <input type="submit" class="btn btn-brand-secondary" value="Search" />
        </p>
    </form>

    @if (Model.HasQuery && !Model.HasResults)
    {
        <p>No results found. Try <a href="@Url.Action(actionName: "Pending")">pending validations</a>.</p>
    }
    else if (!Model.HasQuery)
    {
        <p>You can search by:</p>
        <ul>
            <li>Package ID: <code>NuGet.Versioning</code></li>
            <li>Package ID and version: <code>NuGet.Versioning 4.3.0</code></li>
            <li>Validation set tracking ID: <code>e6db6737-bd77-44cd-af8b-74826e054d36</code></li>
            <li>Validation ID: <code>218d075b-99b6-4151-ba12-121872ffe7fe</code></li>
        </ul>
    }

    @foreach (var package in Model.Packages)
    {
        <div>
            <h3>
                <a href="@Url.Action("Search", new { q = package.Id } )">@package.Id</a>
                <a href="@Url.Action("Search", new { q = package.Id + " " + package.NormalizedVersion } )">@package.NormalizedVersion</a>
            </h3>
            <p>
                @switch (package.DeletedStatus)
                {
                    case PackageDeletedStatus.Unknown:
                        <i>This @package.ValidatingType.ToString().ToLower() was hard deleted.</i><br />
                        break;
                    case PackageDeletedStatus.SoftDeleted:
                        <i>This @package.ValidatingType.ToString().ToLower() was soft deleted.</i><br />
                        break;
                }

                @switch (package.ValidatingType)
                {
                    case ValidatingType.SymbolPackage:
                        <b>Symbol Package Key:</b> @package.PackageKey<br />
                        break;

                    default:
                        <b>@package.ValidatingType Key:</b> @package.PackageKey<br />
                        break;
                }

                @if (package.DeletedStatus == PackageDeletedStatus.NotDeleted)
                {
                    if (package.ValidatingType == ValidatingType.Package)
                    {
                        @ViewHelpers.PostLink(
                            this,
                            formId: "revalidate-package-form-" + package.PackageKey,
                            linkText: "Revalidate package",
                            actionName: "Revalidate",
                            controllerName: "Packages",
                            role: string.Empty,
                            formValues: new Dictionary<string, string>
                            {
                                { "id", package.Id },
                                { "version", package.NormalizedVersion },
                                { "returnUrl", Url.Current() },
                            })
                        <br />
                    }
                    else if (package.ValidatingType == ValidatingType.SymbolPackage)
                    {
                        @ViewHelpers.PostLink(
                            this,
                            formId: "revalidate-symbols-form-" + package.PackageKey,
                            linkText: "Revalidate symbols",
                            actionName: "RevalidateSymbols",
                            controllerName: "Packages",
                            role: string.Empty,
                            formValues: new Dictionary<string, string>
                            {
                                { "id", package.Id },
                                { "version", package.NormalizedVersion },
                                { "returnUrl", Url.Current() },
                            })
                        <br />
                    }
                }

                @if (package.DeletedStatus != PackageDeletedStatus.Unknown)
                {
                    <a href="@Url.Package(package)">Package details</a>
                }
            </p>

            @foreach (var set in package.ValidationSets)
            {
                <h4>Validation Set @set.Key</h4>
                <div style="margin-left: 20px">
                    <p>
                        <b>Tracking ID:</b> <a href="@Url.Action("Search", new { q = set.ValidationTrackingId } )">@set.ValidationTrackingId</a><br />
                        <b>Created:</b> @ShowTimestamp(set.Created)<br />
                        @if (set.Created != set.Updated)
                        {
                            <b>Updated:</b> @ShowTimestamp(set.Updated)<br />
                        }
                        <b>Status:</b>
                        @switch (set.ValidationSetStatus)
                        {
                            case ValidationSetStatus.Unknown:
                                @:Unknown
                                break;
                            case ValidationSetStatus.InProgress:
                                @:In progress
                                break;
                            case ValidationSetStatus.Completed:
                                @:Complete
                                break;
                            default:
                                @(set.ValidationSetStatus)
                                break;
                        }
                    </p>

                    @if (!set.PackageValidations.Any())
                    {
                        <p>This validation set's validation records have not been created yet.</p>
                    }
                    else
                    {
                        <table class="table" style="width: 100%" aria-label="Package Validations">
                            <thead>
                                <tr>
                                    <th>Key</th>
                                    <th>Type</th>
                                    <th>Started</th>
                                    <th>Status</th>
                                    <th>Status Timestamp</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var validation in set.PackageValidations)
                                {
                                    <tr>
                                        <td>@validation.Key</td>
                                        <td>@validation.Type</td>
                                        <td>@ShowTimestamp(validation.Started)</td>
                                        <td>
                                            @switch (validation.ValidationStatus)
                                            {
                                                case ValidationStatus.Failed:
                                                    @:Failed
                                                    break;
                                                case ValidationStatus.Incomplete:
                                                    @:Incomplete
                                                    break;
                                                case ValidationStatus.NotStarted:
                                                    @:Not started
                                                    break;
                                                case ValidationStatus.Succeeded:
                                                    @:Succeeded
                                                    break;
                                                default:
                                                    @(validation.ValidationStatus)
                                                    break;
                                            }
                                        </td>
                                        <td>@ShowTimestamp(validation.ValidationStatusTimestamp)</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                </div>

            }
        </div>
    }
</section>

@section BottomScripts {
    <script>
    $(document).ready(function () {
        $('#revalidate-pending-packages, #revalidate-pending-symbol-packages').submit(function (e) {
            var operation = $(".post-link[data-form-id='" + e.target.id + "']").text().toLowerCase();
            if (!operation || !confirm('Are you sure you want to ' + operation + '?')) {
                e.preventDefault();
            }
        });
    });
    </script>
}
